<?php

class RequestedPage {

    private $content;
    public function getContent() { return $this->content; }

    /**
     * http://host.ru/section/page.php?param=1#ancor
     */
    private $requestedUrl;
    public function getRequestedUrl() { return $this->requestedUrl; }

    /**
     * /section/page.php?param=1
     */
    private $requestedPath;
    public function getRequestedPath() { return $this->requestedPath; }

    private $requestedPathWithoutRedirect;
    public function getRequestedPathWithoutRedirect() { return $this->requestedPathWithoutRedirect; }

    private $basePath;
    public function getBasePath() { return $this->basePath; }

    private $header;
    public function getContentType() {
        return preg_match('~Content-Type: ([^\s;]*)~', $this->header, $m) ?
            $m[1] :
            'unknown';
    }
    public function isHtml() {
        return $this->getContentType() == 'text/html';
    }

    function __construct(
        $requestedUrlWithoutRedirect,
        $requestedUrl,
        $content,
        $header//,
//        $redirectUrl
    ) {
        $this->requestedUrl = $requestedUrl;
        //$this->redirectUrl  = $redirectUrl;
        $this->content      = $content;
        $this->header       = $header;

        $this->requestedPath = self::getPathOnlyFromUrl($this->requestedUrl);
        $this->requestedPathWithoutRedirect = self::getPathOnlyFromUrl($requestedUrlWithoutRedirect);
        $this->basePath      = self::getBasePathFromPath($this->requestedPath);
        //$this->redirectUrl   = self::getPathOnlyFromUrl($this->redirectUrl);
    }

    private static function getPathOnlyFromUrl($url) {
        return preg_match('~^[^:]*://[^/]*([^#]*)~', $url, $m) ?
            $m[1] :
            $url;
    }

    private static function getBasePathFromPath($path) {
        if (!preg_match('~^([^#?]*)~', $path, $m))
            throw new Exception("Can not get basePath from '$path'");
        return $m[1];
    }

    public function getContentDescription() {

        if($this->isHtml()) {
            return (preg_match('~<title>(.*)</title>~isuU', $this->content, $m)) ?
                $m[1] :
                '*** ERROR *** html file without "title" tag: ' . mb_substr($this->content, 0, 300);
        }

        return $this->getContentType();
    }

    public function getContentSize() {
        return self::formattedFilesize(strlen($this->content));
    }

    private static function formattedFilesize($size, $words = null) {

        if (!$words) $words = array(' Kb', ' Mb', ' G');

        $GB = 1073741824;
        $MB = 1048576;
        $KB = 1024;

        if ($size >= $GB*10) {
            return (int)($size / $GB).$words[2];
        } else if ($size >= $GB) {
            return ((int)($size / $GB*10) / 10).$words[2];
        } else if ($size >= $MB*10) {
            return (int)($size / $MB).$words[1];
        } else if ($size >= $MB/5) {
            return ((int)($size / $MB*10) / 10).$words[1];
        } else if ($size >= $KB) {
            return (int)($size / $KB).$words[0];
        } else if ($size >= $KB/5) {
            return ((int)($size / $KB*10) / 10).$words[0];
        } else {
            return $size;
        }
    }

}



?>